<%@ page import="com.example.myproject.*" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.images.Image" %>

<%
	String filetype = request.getParameter("fileType")==null?"":request.getParameter("fileType");
	String sorttype = request.getParameter("sortType")==null?"":request.getParameter("sortType");
	String searchType = request.getParameter("typeOfSearch")==null?"":request.getParameter("typeOfSearch");
	String content = request.getParameter("content")==null?"":request.getParameter("content");
	
	String searchContent = "(?i).*" + content + ".*";

	if(!filetype.equals(""))
	{
%>
	<!--  To searching [-->		
	
		<form name="Search" method="get">
		<table border=0 style="padding:0px 10px 5px 0px; color:#1BA6B2; margin-bottom:5px">
		<tr>
			<th>
			<select name="typeOfSearch">
				<option value="Title" <%=searchType.equals("Title")?"selected":"" %>>Title</option>
			<% if("audio".equals(filetype)){ %>			    
			    <option value="Album" <%=searchType.equals("Album")?"selected":"" %>>Album</option>
			 <%}else if("video".equals(filetype)){ %>
			    <option value="Episode" <%=searchType.equals("Episode")?"selected":"" %>>Episode</option>
			    <option value="Series" <%=searchType.equals("Series")?"selected":"" %>>Series</option>
			 <%}%>
			</select>
			</th>
			<th><input type="text" name="content" value=<%=content %> ></th> 		
			<input type=hidden name="fileType" value="<%=filetype%>">	
			<input type=hidden name="sortType" value="<%=sorttype%>">	
			<th colspan="2"><input type="submit" value="Search"/></th>
			</tr>
			</table>
		</form>
<!--]  To searching -->	
		<form method="POST" action="list.jsp" name="form1">
		<input type=hidden name="fileType" value="<%=filetype%>">
		<input type=hidden name="sortType" value="<%=sorttype%>">
		<input type=hidden name="id">
		<input type=hidden name="parentid">

		<table border=1 cellpadding=10 cellspacing =0 >
		<tr>	
<%
		List<?> alist = null;

		if("audio".equals(filetype))
		{
			 AudioUploadDAO dao = new AudioUploadDAO();

			if(!sorttype.equals(""))
			{
				alist = dao.listByGenre(sorttype);
			}
			else
			{
				alist = dao.list();
			}
		}
		else if("video".equals(filetype))
		{
			VideoUploadDAO dao = new VideoUploadDAO();
			if(!sorttype.equals(""))
			{
				alist = dao.listByCaption(sorttype);
			}
			else
			{
				alist = dao.list();
			}			
		}
		else if("image".equals(filetype))
		{
			PictureUploadDAO dao = new PictureUploadDAO();
			if(!sorttype.equals(""))
			{
				alist = dao.listByCaption(sorttype);
			}
			else
			{
				alist = dao.list();
			}					
		}
		else if("book".equals(filetype))
		{
			BookUploadDAO dao = new BookUploadDAO();
			alist = dao.list();			
		}
		else 
		{
			UploadDAO dao = new UploadDAO();
			alist = dao.list();			
		}

		String title = "";
		String caption = "";
		String contenttype = "";
		
		long id = 0; 
		long parentid = 0;
		long rowid = 0;
		long thumbid = 0;
		
		List<?> finalList = null;

		
		if(alist.size()>0)
		{
			for(int i=0; i<alist.size(); i++)
			{
				
				if(alist.get(i) instanceof AudioUploadDTO )
				{
					
					//title search
					if(searchType.equals(""))
					{
						title = ((AudioUploadDTO)alist.get(i)).getTitle();
						id = ((AudioUploadDTO)alist.get(i)).getId();
						parentid = ((AudioUploadDTO)alist.get(i)).getParentId();
						thumbid = ((AudioUploadDTO)alist.get(i)).getThumbnailId()==null?0:((AudioUploadDTO)alist.get(i)).getThumbnailId();
						
					}
					else if(searchType.equals("Title") && ((AudioUploadDTO)alist.get(i)).getTitle().matches(searchContent))
					{
						
						title = ((AudioUploadDTO)alist.get(i)).getTitle();
						id = ((AudioUploadDTO)alist.get(i)).getId();
						parentid = ((AudioUploadDTO)alist.get(i)).getParentId();
						thumbid = ((AudioUploadDTO)alist.get(i)).getThumbnailId()==null?0:((AudioUploadDTO)alist.get(i)).getThumbnailId();
					}
					else if(searchType.equals("Album") && ((AudioUploadDTO)alist.get(i)).getAlbum().matches(searchContent))
					{
						title = ((AudioUploadDTO)alist.get(i)).getTitle();
						id = ((AudioUploadDTO)alist.get(i)).getId();
						parentid = ((AudioUploadDTO)alist.get(i)).getParentId();
						thumbid = ((AudioUploadDTO)alist.get(i)).getThumbnailId()==null?0:((AudioUploadDTO)alist.get(i)).getThumbnailId();
					}
				
				}
				else if(alist.get(i) instanceof VideoUploadDTO)
				{
					if(searchType.equals(""))
					{
						title = ((VideoUploadDTO)alist.get(i)).getTitle();
						id = ((VideoUploadDTO)alist.get(i)).getId();
						parentid = ((VideoUploadDTO)alist.get(i)).getParentId();
						thumbid = ((VideoUploadDTO)alist.get(i)).getThumbnailId()==null?0:((VideoUploadDTO)alist.get(i)).getThumbnailId();
					}
					else if(searchType.equals("Title") && ((VideoUploadDTO)alist.get(i)).getTitle().matches(searchContent))
					{
						title = ((VideoUploadDTO)alist.get(i)).getTitle();
						id = ((VideoUploadDTO)alist.get(i)).getId();
						parentid = ((VideoUploadDTO)alist.get(i)).getParentId();
						thumbid = ((VideoUploadDTO)alist.get(i)).getThumbnailId()==null?0:((VideoUploadDTO)alist.get(i)).getThumbnailId();
					}
					else if(searchType.equals("Episode") && ((VideoUploadDTO)alist.get(i)).getEpisode().matches(searchContent))
					{
						title = ((VideoUploadDTO)alist.get(i)).getTitle();
						id = ((VideoUploadDTO)alist.get(i)).getId();
						parentid = ((VideoUploadDTO)alist.get(i)).getParentId();
						thumbid = ((VideoUploadDTO)alist.get(i)).getThumbnailId()==null?0:((VideoUploadDTO)alist.get(i)).getThumbnailId();
					}
					else if(searchType.equals("Series") && ((VideoUploadDTO)alist.get(i)).getSeries().matches(searchContent))
					{
						title = ((VideoUploadDTO)alist.get(i)).getTitle();
						id = ((VideoUploadDTO)alist.get(i)).getId();
						parentid = ((VideoUploadDTO)alist.get(i)).getParentId();
						thumbid = ((VideoUploadDTO)alist.get(i)).getThumbnailId()==null?0:((VideoUploadDTO)alist.get(i)).getThumbnailId();
					}

				}
				else if(alist.get(i) instanceof PictureUploadDTO)
				{
					if(searchType.equals(""))
					{
						title = ((PictureUploadDTO)alist.get(i)).getTitle();
						id = ((PictureUploadDTO)alist.get(i)).getId();
						parentid = ((PictureUploadDTO)alist.get(i)).getParentId();
						thumbid = ((PictureUploadDTO)alist.get(i)).getThumbnailId()==null?0:((PictureUploadDTO)alist.get(i)).getThumbnailId();
					}
					else if(searchType.equals("Title") && ((PictureUploadDTO)alist.get(i)).getTitle().matches(searchContent))
					{
						title = ((PictureUploadDTO)alist.get(i)).getTitle();
						id = ((PictureUploadDTO)alist.get(i)).getId();
						parentid = ((PictureUploadDTO)alist.get(i)).getParentId();
						thumbid = ((PictureUploadDTO)alist.get(i)).getThumbnailId()==null?0:((PictureUploadDTO)alist.get(i)).getThumbnailId();
					}
			
					
				}
				else if(alist.get(i) instanceof BookUploadDTO)
				{
					if(searchType.equals(""))
					{
						title = ((BookUploadDTO)alist.get(i)).getTitle();
						id = ((BookUploadDTO)alist.get(i)).getId();
						parentid = ((BookUploadDTO)alist.get(i)).getParentId();	
						thumbid = ((BookUploadDTO)alist.get(i)).getThumbnailId()==null?0:((BookUploadDTO)alist.get(i)).getThumbnailId();
					}
					else if(searchType.equals("Title") && ((BookUploadDTO)alist.get(i)).getTitle().matches(searchContent))
					{
						title = ((BookUploadDTO)alist.get(i)).getTitle();
						id = ((BookUploadDTO)alist.get(i)).getId();
						parentid = ((BookUploadDTO)alist.get(i)).getParentId();
						thumbid = ((BookUploadDTO)alist.get(i)).getThumbnailId()==null?0:((BookUploadDTO)alist.get(i)).getThumbnailId();	
					}
				}
				
				if(!"".equals(title))
				{
						String thumbnailUrl = "";
					
						if(thumbid != 0)
						{	
							ImageShow image = new ImageShow();
							thumbnailUrl = image.show(filetype, thumbid); 
							 
						}
%>
					<td width=170 height="170" align=center >
					<table width="170" border="0" cellspacing="0" cellpadding="0" style="table-layout:fixed;">
						<tr>
						<td>
						<table width="170" border="0" cellspacing="0" cellpadding="0">
					      <tr>
					        <td height="1" bgcolor="#67666b"></td>
					        <td height="1" bgcolor="#67666b"></td>
					        <td height="1" bgcolor="#67666b"></td>
					      </tr>
					      <tr>
					        <td width="1" bgcolor="#67666b"></td>
					        <td width="168" height="168" bgcolor="#2d2c2f" align="center">
					        <a href="javascript:openURL(<%=parentid%>);")>
							<% if (!thumbnailUrl.equals("")){  %>
							<img src="<%=thumbnailUrl%>=s165" ></br>
							<%}else{%>
							no image
							<%} %>
							</a>
					        </td>
					        <td bgcolor="#67666b"></td>
					      </tr>
					      <tr>
					        <td width="1" bgcolor="#67666b"></td>
					        <td width="168" height="1" bgcolor="#67666b"></td>
					        <td bgcolor="#67666b"></td>
					      </tr>
					    </table>	
						</td>
						</tr>
						<tr>
    					<td bgcolor="#363539">&nbsp;</td>
  						</tr>
						<tr>
		    				<td bgcolor="#363539" align=center>
		    				<a href="javascript:openURL(<%=parentid%>);")><%=title %></a></td>
		  				</tr>
		  				<tr>
    					<td bgcolor="#363539">&nbsp;</td>
  						</tr>
						<tr>
							<td width= 170 height="35" bgcolor="#363539" align=center>
						    <table border="0" cellspacing="0" cellpadding="0">
					    
						    
						      <tr>
						        <th width="51" scope="row"><a href="/thumbnailUpload.jsp?id=<%=id%>&fileType=<%=filetype%>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('button-thum<%=i%>','','images/button_thum_ov.gif',1)"><img src="images/button_thum.gif" name="button-thum<%=i%>" width="51" height="35" border="0" id="button-thum<%=i%>" /></a></th>
						        <td width="6">&nbsp;</td>
						        <td width="35"><a href="javascript:goDownLoad(<%=id%>, <%=parentid%>);"  onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('button-down<%=i%>','','images/button_down_ov.gif',1)"><img src="images/button_down.gif" name="button-down<%=i%>" width="35" height="35" border="0" id="button-down<%=i%>" /></a></td>
						        <td width="5">&nbsp;</td>
						        <td width="35"><a href="edit.jsp?id=<%=id %>&fileType=<%=filetype%>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('button-edit<%=i%>','','images/button_edit_ov.gif',1)"><img src="images/button_edit.gif" name="button-edit<%=i%>" width="35" height="35" border="0" id="button-edit<%=i%>" /></a></td>
						        <td width="4">&nbsp;</td>
						        <td width="35"><a href="javascript:goDelete(<%=id%>, <%=parentid%>);" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('button-del<%=i%>','','images/button_del_ov.gif',1)"><img src="images/button_del.gif" name="button-del<%=i%>" width="35" height="35" border="0" id="button-del<%=i%>" /></a></td>
						     </tr>
						    </table>
						    </td>
						</tr>
					</table>
					</td>
<%
					title = "";
					rowid++;
					
					if(rowid % 3 == 0)
					{
%>
					</tr>
					<tr>
<% 
					}
				}
			}
		
		}
		%>
		</tr>
		<%
		
		// result list == 0 
		if(rowid <= 0)
		{	
%>
				<tr>	
					<th colspan=5 align=center>no data</th>	

				</tr>
			</table>
	</form>
<%
		}
	}
%>	
